import java.text.DecimalFormat;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;

/**
 * IDE: Eclipse
 *
 * @author Justin Niles
 * @author Grant Foster
 * @author Bryce Langlotz
 * @author Max Honosky
 *
 */

public class WriteToFile
{
    private static String newLine = System.getProperty("line.separator");

    public static void writeToFile(ArrayList<Point> cavityList)
    {
        System.out.println("Number of Cavities: " + cavityList.size());

        //First get the reference the the actual file path
        File output = new File(Main.outputFilePath);
        try
        {
            if (output.exists())
            {
                output.delete();
            }
            output.createNewFile();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        //Also get a reference to the input file so that it can be written
        //first to the output file
        File input = new File(Main.inputFilePath);
        String inputToString = "";
        try
        {
            FileInputStream fis = new FileInputStream(input);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            inputToString = new String(buffer);
        }
        catch (Exception e1){
            e1.printStackTrace();
        }

        //Create the string to add to the end that lists the cavities
        String cavityString = "";
        int count = 1;
        for (Point point : cavityList)
        {
            //First, put the count number in a string
            String countS = "";
            if (count < 10)
            {
                countS = "00" + count;
            }
            else if (count < 100)
            {
                countS = "0" + count;
            }
            else
            {
                countS = "" + count;
            }
            //Now get the x, y, and z values (offset)
            String xSpace = "";
            String ySpace = "";
            String zSpace = "";
            float xpos = point.xpos*Main.resolution + CavityCalculator.xMin;
            float ypos = point.ypos*Main.resolution + CavityCalculator.yMin;
            float zpos = point.zpos*Main.resolution + CavityCalculator.zMin;
            DecimalFormat df = new DecimalFormat("#0.000");
            String xString = df.format(xpos);
            if (xString.length()<7)
            {
                while(xString.length() < 7)
                {
                    xString = " " + xString;
                }
            }
            String yString = df.format(ypos);
            if (yString.length()<7)
            {
                while(yString.length() < 7)
                {
                    yString = " " + yString;
                }
            }
            String zString = df.format(zpos);
            if (zString.length()<7)
            {
                while(zString.length() < 7)
                {
                    zString = " " + zString;
                }
            }
            cavityString = cavityString + String.format(
                "ATOM  10%s  MC  CAV  5%s     %s %s %s  1.00  %1.2f",
                countS, countS, xString, yString, zString, Main.probeRadius);
            cavityString = cavityString + newLine;
            count++;
        }

        //Now create the FileWriter and actually write to file
        FileWriter fileWriter;
        try
        {
            System.out.println("\nWriting to file...");
            fileWriter = new FileWriter(output);
            fileWriter.write(inputToString);
            fileWriter.write(cavityString);
            fileWriter.close();
            System.out.println("File Complete.");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
